/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package deti.es.ukni.cm.account;

import deti.es.persistence.account.AccountManagerRemote;
import deti.es.ukni.cm.persistence.DAOFactory;

import deti.es.ukni.persistence.entity.Account;
import deti.es.ukni.persistence.entity.Band;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet to create a new Account.
 *
 * @author ukni
 */
public class CreateAccount extends HttpServlet {

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        // servelet para criar uma nova conta..
        HttpSession session = request.getSession(true);
        AccountManagerRemote AMBean = DAOFactory.getAccountManager();
        Account newAccount;
        Band newBand;

        // Tentar criar uma conta com email repetido...
        newAccount = AMBean.createAccount( request.getParameter("username") ,
                                           request.getParameter("email"),
                                           request.getParameter("pwd"), 3);

        if ( newAccount == null )
            System.out.println("Algo de errado aconteceu");

        if ( "on".equals(request.getParameter("atype")) == false ){
            session.setAttribute("message", "account created with success!");
            response.sendRedirect("./login.jsp");
            return;
        }
        
        DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
        Date formation;
        try {
            formation = df.parse(request.getParameter("bfdate"));
        } catch (ParseException ex) {
            formation = new Date();
        }

        String desc = request.getParameter("bdesc");
        String bio  = request.getParameter("bbio");

        if ( desc == null || desc.equals("") )
            desc = "Insert here your band description";
        if ( bio == null || bio.equals("") )
            bio = "Insert here your band biography";

        newBand = AMBean.createBand(newAccount, request.getParameter("bname"),
                                    formation, desc,
                                    bio);

        session.setAttribute("message", "account created with success!");
        response.sendRedirect("./login.jsp");

    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
